<template>
  <LoginHeader>联合登录</LoginHeader>
  <section class="container">
    <nav class="tab">
      <a
        href="javascript:;"
        @click="activeCom = false"
        :class="{ active: activeCom === false }"
      >
        <i class="iconfont icon-bind" />
        <span>已有小兔鲜账号，请绑定手机</span>
      </a>
      <a
        href="javascript:;"
        @click="activeCom = true"
        :class="{ active: activeCom === true }"
      >
        <i class="iconfont icon-edit" />
        <span>没有小兔鲜账号，请完善资料</span>
      </a>
    </nav>
    <div class="tab-content">
      <!-- 动态组件 -->
      <keep-alive>
        <component :unionId="unionId" :is="activeCom ? CallbackBind : CallbackPatch"></component>
      </keep-alive>
    </div>
  </section>
  <LoginFooter />
</template>

<script setup lang="ts">
import LoginHeader from './components/login-header.vue'
import LoginFooter from './components/login-footer.vue'
import CallbackBind from './components/callback-bind.vue'
import CallbackPatch from './components/callback-patch.vue'
import { ref } from 'vue'
import { reqUserQQLogin } from '@/api/user'
import { useStore } from '@/store'
import { message } from '@/components/Message'
import { useRouter } from 'vue-router'

const { user, cart } = useStore()
const router = useRouter()

const activeCom = ref(true)
const unionId = ref('')
// QC.Login.check()  检查 QQ 是否登录，true 表示已登录
if (QC.Login.check()) {
  // 获取当前登录用户的 openId：用户身份的唯一标识
  QC.Login.getMe(async (openId: string) => {
    console.log('获取到QQopenId：用户身份的唯一标识', openId)
    unionId.value = openId
    // 根据 unionId/openId 直接登录
    const { result } = await reqUserQQLogin(openId)
    // 1. 存储用户信息
    await user.setProfile(result)
    // 🔔 合并购物车
    await cart.mergeLocalCart()
    // 2. 跳转页面
    router.push('/')
    // 3. 提示
    message({ type: 'success', text: 'QQ授权登录成功' })
  })
}
</script>

<style scoped lang="scss">

.container {
  padding: 25px 0;
}
.tab {
  background: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: $xtxColor;
      border-color: $xtxColor;
    }
  }
}
.tab-content {
  min-height: 600px;
  background: #fff;
}
</style>
